package com.my.study.algorithm.leetcode;

/**
 * 改变矩阵维度
 * <p>
 *     比如原来有一个 3 * 2的二维数组，输入一个n和m，将数组变成n*m的二维数组
 * </p>
 * 
 * @author Carlos
 * @version 1.0.0
 * @date 2023/2/23 7:56
 */
public class MatrixReshape {
    
    
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int sRow = nums.length;
        int sColumn = nums[0].length;
        
        if (sRow * sColumn != r * c) {
            return nums;
        }
        
        int index = 0;
        int[][] reshapeNums = new int[r][c];
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                reshapeNums[i][j] = nums[index / sColumn][index % sColumn];
            }
        }
        
        return reshapeNums;
    }
    
}
